﻿using MelodysDemo.Application.Clazzs;

namespace MelodysDemo.API.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class ClazzsController : Controller
    {
        readonly IRepository<SurveyClazz, int> _surveyClazz;//班级
        readonly IClazzService _clazzService;
        public ClazzsController(IRepository<SurveyClazz, int> surveyClazz,
                                IClazzService clazzService)
        {
            _surveyClazz = surveyClazz;
            _clazzService = clazzService;
        }
        //查询
        [HttpPost]
        public async Task<IActionResult> Clazzs([FromBody] ClazzInput input)
        {
            var list = await _clazzService.GetResultDto(input);
            return Json(new VueResult<SurveyClazz> { code = 0, msg = "请求成功", count = list.TotalCount, data = list.Data });
        }

        //新增
        [HttpPost]
        public async Task<IActionResult> ClazzAdd([FromBody] SurveyClazz clazz)
        {
            SurveyClazz ccname = await _surveyClazz.FirstOrDefaultAsync(c => c.ClazzName == clazz.ClazzName);
            if (ccname != null)
                return Json(new UIResult(-1, "该班级名已被使用"));
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            clazz.Version = 1;//版本号
            clazz.Creator = uname;
            clazz.CreateTime = DateTime.Now;//创建时间
            clazz.Modifier = uname;
            clazz.ModifiedTime = DateTime.Now;//修改时间
            clazz.IsDelete = 0;//默认不删除
            SurveyClazz c = await _surveyClazz.InsertAsync(clazz);
            if (c != null)
                return Json(new UIResult(0, "添加成功"));
            return Json(new UIResult(-1, "添加失败"));
        }

        //修改
        [HttpPatch]
        public async Task<IActionResult> ClazzEdit([FromBody] SurveyClazz clazz)
        {
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            var obj = await _surveyClazz.FirstOrDefaultAsync(c => c.Id == clazz.Id);
            obj.ClazzName = clazz.ClazzName;
            obj.AdviserName = clazz.AdviserName;//班主任
            obj.TeacherName = clazz.TeacherName;//授课老师
            obj.StudentCount = clazz.StudentCount;
            obj.Status = clazz.Status;
            obj.Modifier = uname;
            obj.ModifiedTime = DateTime.Now;//修改时间
            obj.Version++;
            SurveyClazz r = await _surveyClazz.UpdateAsync(obj);
            if (r != null)
            {

                return Json(new UIResult(0, "修改成功"));
            }
            return Json(new UIResult(-1, "修改失败"));
        }

        //删除
        [HttpGet]
        public async Task<IActionResult> ClazzDel([FromQuery] int id)
        {
            var obj = await _surveyClazz.FirstOrDefaultAsync(c => c.Id == id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            obj.IsDelete = 1;//修改删除状态
            obj.Modifier = uname;
            obj.ModifiedTime = DateTime.Now;
            obj.Version++;
            SurveyClazz? r = await _surveyClazz.UpdateAsync(obj);
            if (r != null)
            {

                return Json(new UIResult(0, "删除成功"));
            }
            return Json(new UIResult(-1, "删除失败"));
        }

    }
}
